package com.wctj.api.business.common.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wctj.api.base.entity.DdwxPayOrder;
import com.wctj.api.base.enums.OrderTypeEnums;
import com.wctj.api.base.enums.PayTypeEnums;
import com.wctj.api.base.enums.PlatformEnums;
import com.wctj.api.base.utils.InstanceConvertUtil;
import com.wctj.api.base.utils.OrderIdUtils;
import com.wctj.api.business.common.dao.DdwxPayOrderMapper;
import com.wctj.api.business.common.dto.DdwxMemberDTO;
import com.wctj.api.business.common.dto.DdwxPayOrderDTO;
import com.wctj.api.business.common.dto.DdwxRechargeOrderDTO;
import com.wctj.api.business.common.service.DdwxPayOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class DdwxPayOrderServiceImpl extends ServiceImpl<DdwxPayOrderMapper, DdwxPayOrder> implements DdwxPayOrderService {


    @Override
    public DdwxPayOrderDTO findByOrderNum(String orderNum) {
        DdwxPayOrder ddwxPayOrder = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxPayOrder::getOrdernum, orderNum)
                .one();
        return InstanceConvertUtil.convertObject(ddwxPayOrder, DdwxPayOrderDTO.class);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public DdwxPayOrderDTO addCmbRechargeRecord(DdwxMemberDTO ddwxMemberDTO, String amount) {
        DdwxPayOrder ddwxPayOrder = new DdwxPayOrder();
        ddwxPayOrder.setAid(ddwxMemberDTO.getAid());
        ddwxPayOrder.setBid(ddwxMemberDTO.getBid());
        ddwxPayOrder.setMid(ddwxMemberDTO.getId());
        //ddwxPayOrder.setOrderid(); 从充值订单表回填记录的主键id  离谱 还要这样操作
        ddwxPayOrder.setOrdernum(OrderIdUtils.getOrderId());
        ddwxPayOrder.setTitle("余额充值");
        ddwxPayOrder.setMoney(new BigDecimal(amount));
        ddwxPayOrder.setScore(0);
        ddwxPayOrder.setType(OrderTypeEnums.recharge.getCode());
        ddwxPayOrder.setCreatetime((int) System.currentTimeMillis());
        ddwxPayOrder.setStatus(1); // *0未支付 1已支付 2已取消
        //ddwxPayOrder.setPaynum();
        ddwxPayOrder.setPaytypeid(PayTypeEnums.cmbpay.getCode());
        ddwxPayOrder.setPaytype(PayTypeEnums.cmbpay.getMsg());
        ddwxPayOrder.setPaytime((int) System.currentTimeMillis());
        ddwxPayOrder.setPlatform(PlatformEnums.cmb.name());
        baseMapper.insert(ddwxPayOrder);
        return InstanceConvertUtil.convertObject(ddwxPayOrder, DdwxPayOrderDTO.class);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateOrderId(DdwxPayOrderDTO ddwxPayOrderDTO, DdwxRechargeOrderDTO ddwxRechargeOrderDTO) {
        ddwxPayOrderDTO.setOrderid(ddwxRechargeOrderDTO.getId());
        DdwxPayOrder ddwxPayOrder = InstanceConvertUtil.convertObject(ddwxPayOrderDTO, DdwxPayOrder.class);
        baseMapper.updateById(ddwxPayOrder);
    }


}
